package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.DtsTopic;
import com.qf.service.DtsTopicService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * author:z
 * time:2021/11/30 17:34
 * description:
 */
@RestController
@CrossOrigin("*")
@RequestMapping("/admin/topic")
public class AdminTopicController {

    @Autowired
    DtsTopicService topicService;

    @RequestMapping("/delete")
    public Object delete(@RequestBody DtsTopic dtsTopic) {
        topicService.delete(dtsTopic);
        return ResponseUtil.ok();
    }

    @RequestMapping("/update")
    public Object update(DtsTopic dtsTopic) {
        topicService.update(dtsTopic);
        return ResponseUtil.ok();
    }


    @RequestMapping("/create")
    public Object add(@RequestBody DtsTopic topic) {
        DtsTopic data = topicService.add(topic);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/list")
    public Object list(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                       @RequestParam(name = "limit", required = false, defaultValue = "20") Integer limit,
                       @RequestParam(name = "sort", required = false, defaultValue = "add_time") String sort,
                       @RequestParam(name = "order", required = false, defaultValue = "desc") String order,
                       String title,
                       String subtitle) {
        IPage<DtsTopic> iPage = topicService.queryPage(page, limit, sort, order, title, subtitle);

        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }
}
